//
//  STTools_UserManger.m
//  GodHorses
//
//  Created by coder on 2017/11/25.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "STTools_UserManger.h"
#import "STTools_QnTokenCreateManger.h"
#define STTOOLS_USER_JSON_KEY @"STTOOLS_USER_JSON_KEY"
#define STTOOLS_USER_ACCOUNT_KEY @"STTOOLS_USER_ACCOUNT_KEY"
#define STTOOLS_USER_PWD_KEY @"STTOOLS_USER_PWD_KEY"
#define STTOOLS_TEMP_USER_JSON_KEY @"STTOOLS_TEMP_USER_JSON_KEY"
#define STTOOLS_USER_NO_LOGIN_TOKEN @"STTOOLS_USER_NO_LOGIN_TOKEN"
@implementation STTools_UserManger
+ (STTools_UserManger *)defult{
    static STTools_UserManger * stoneobs_temp_deflut = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        stoneobs_temp_deflut = [STTools_UserManger new];
    });
    return stoneobs_temp_deflut;
}
- (NSString *)noLoginToken{
//    NSString * key = [NSBundle stoneobs_fun_st_readUUIDFromKeyChain];
    //更改 不从钥匙串，只存沙盒
    NSString * stoneobs_temp_key = [[NSUserDefaults standardUserDefaults] valueForKey:STTOOLS_USER_NO_LOGIN_TOKEN];
    return stoneobs_temp_key;
}
- (void)stoneobs_fun_removeUserPreferce{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:STTOOLS_USER_JSON_KEY];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:STTOOLS_USER_ACCOUNT_KEY];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:STTOOLS_USER_PWD_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)stoneobs_fun_updateUserModel:(THUserModel *)userModel{
    if (userModel) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:STTOOLS_TEMP_USER_JSON_KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
        if ([self stoneobs_fun_get_loginedUser]) {
            //不更新token
            if (userModel.token.length) {
                
            }else{
               userModel.token = [self stoneobs_fun_get_loginedUser].token;
            }
            
        }
        [RCIM sharedRCIM].currentUserInfo = userModel.userInfo;
        [RCIM sharedRCIM].enableMessageAttachUserInfo = YES;
        
        NSString * userString = userModel.mj_JSONString;
        [[NSUserDefaults standardUserDefaults] setValue:userString forKey:STTOOLS_USER_JSON_KEY];
    }else{
        NSLog(@"user 不能为空");
    }
}
- (void)stoneobs_fun_update_tempUserModel:(THUserModel*)userModel{
    if (userModel) {
        if ([self stoneobs_fun_get_loginedUser]) {
            //不更新token
            if (userModel.token.length) {
                
            }else{
               userModel.token = [self stoneobs_fun_get_loginedUser].token;
            }

        }
        [RCIM sharedRCIM].currentUserInfo = userModel.userInfo;
        [RCIM sharedRCIM].enableMessageAttachUserInfo = YES;
        
        NSString * userString = userModel.mj_JSONString;
        [[NSUserDefaults standardUserDefaults] setValue:userString forKey:STTOOLS_TEMP_USER_JSON_KEY];
    }else{
        NSLog(@"user 不能为空");
    }
}
- (THUserModel*)stoneobs_fun_get_tempUser{
    NSString * stoneobs_temp_jsonStr = [[NSUserDefaults standardUserDefaults] valueForKey:STTOOLS_TEMP_USER_JSON_KEY];
    NSData * stoneobs_temp_data = [stoneobs_temp_jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    if (stoneobs_temp_jsonStr.length) {
        NSDictionary * stoneobs_temp_dic = [NSJSONSerialization JSONObjectWithData:stoneobs_temp_data options:NSJSONReadingAllowFragments error:nil];
        THUserModel *stoneobs_fun_get_loginedUser = [THUserModel mj_objectWithKeyValues:stoneobs_temp_dic];
        return stoneobs_fun_get_loginedUser;
    }
    return nil;
}
- (THUserModel *)stoneobs_fun_get_loginedUser{
    NSString * stoneobs_temp_jsonStr = [[NSUserDefaults standardUserDefaults] valueForKey:STTOOLS_USER_JSON_KEY];
    NSData * stoneobs_temp_data = [stoneobs_temp_jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    if (stoneobs_temp_jsonStr.length) {
        NSDictionary * stoneobs_temp_dic = [NSJSONSerialization JSONObjectWithData:stoneobs_temp_data options:NSJSONReadingAllowFragments error:nil];
        THUserModel *stoneobs_fun_get_loginedUser = [THUserModel mj_objectWithKeyValues:stoneobs_temp_dic];
        return stoneobs_fun_get_loginedUser;
    }
    return nil;
}

- (void)stoneobs_fun_updateAccount:(NSString *)account pwd:(NSString *)pwd{
    if (account.length && pwd.length) {
        [[NSUserDefaults standardUserDefaults] setValue:account forKey:STTOOLS_USER_ACCOUNT_KEY];
        [[NSUserDefaults standardUserDefaults] setValue:pwd forKey:STTOOLS_USER_PWD_KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
}
- (NSString*)account{
    NSString * account = [[NSUserDefaults standardUserDefaults] valueForKey:STTOOLS_USER_ACCOUNT_KEY];
    return account;
}
- (NSString*)pwd{
    NSString * pwd = [[NSUserDefaults standardUserDefaults] valueForKey:STTOOLS_USER_PWD_KEY];
    return pwd;
}
#pragma mark --NetWork Method
- (void)stoneobs_fun_gerQnInfoRequestWithToken:(NSString*)token{

}
@end

